package com.yida.wy.common.utils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yida.common.core.text.Convert;
import com.yida.common.core.utils.StringUtils;
import com.yida.wy.common.utils.jexl.GwjCalculator;

/***
 * @author lqq
 **/
public class WyCalculateFormulaUtil {

    private static final Logger log = LoggerFactory.getLogger(WyCalculateFormulaUtil.class);

    /**
     * 根据key替换参数形成公式
     *
     * @param formula 公式
     * @param data 请求参数
     * @return /
     */
    public static BigDecimal getFormulaResult(String formula, Map<String, Object> data) {
        String[] f = StringUtils.split(formula, "_");
        StringBuilder r = new StringBuilder();
        for (String s : f) {
            if (StringUtils.isNotNull(data.get(s))) {
                r.append(Convert.toStr(data.get(s)));
            } else {
                r.append(s);
            }
        }
        System.out.println(r.toString());
        return GwjCalculator.conversion(r.toString());
    }

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("totalMoney", 10);
        map.put("totalSize", 3);
        map.put("depositSize", 2);
        System.out.println(getFormulaResult("totalMoney_/_totalSize_*_depositSize", map));
    }
}
